import java.util.ArrayList;
import java.util.List;

/**
 * 1957. 删除字符使字符串变好
 * https://leetcode-cn.com/problems/delete-characters-to-make-fancy-string/
 */
public class Solutions_1957 {
    public static void main(String[] args) {
        String s1 = "leeetcode" ;  // output: "leetcode"
        String s2 = "aaabaaaa" ;  // output: "aabaa"
        String s3 = "aab" ;  // output: "aab"

        List<String> inputs = new ArrayList<>();
        inputs.add(s1);
        inputs.add(s2);
        inputs.add(s3);

        for (String input : inputs) {
            String result = makeFancyString(input);
            System.out.println(result);
        }
    }

    public static String makeFancyString(String s) {
        int idx = 0;
        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if (idx > 1 && arr[idx - 1] == arr[i] && arr[idx - 2] == arr[i]) {
                // 如果当前字符与前 2 字符相同，那么不添加该字符
                continue;
            }
            arr[idx ++] = arr[i];
        }
        String res = String.valueOf(arr, 0, idx);
        return res;
    }
}
